﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbUpgrade.General
{
    public static class Log
    {
        /// <summary>
        /// 锁对象
        /// </summary>
        private static object _lockHelper = new object();

        public static void WriteInfo(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【INFO】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-info", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public static void WriteDebug(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【DEBUG】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-debug", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public static void WriteWarning(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【WARNING】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-warning", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public static void WriteError(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【ERROR】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-error", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public static void WriteException(Exception ex)
        {
            DateTime dt = DateTime.Now;
            string info = "【EXCEPTION】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 异常信息：" + ex.Message + "\r\n堆栈信息：" + ex.StackTrace;
            WriteLog(info, "log-exception", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public static void WriteOnly(string content)
        {
            DateTime dt = DateTime.Now;
            string info = content;
            WriteLog(info, "log-only", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strContent">日志内容</param>
        /// <param name="dir">目录</param>
        /// <param name="filename">文件名称</param>
        private static void WriteLog(string strContent, string dir, string filename)
        {
            try
            {
                lock (_lockHelper)
                {
                    string logFilePath = GetRootMapPath("logs");
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    logFilePath = Path.Combine(logFilePath, dir);
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    string file = Path.Combine(logFilePath, filename);
                    StreamWriter sw = new StreamWriter(file, true, Encoding.UTF8);
                    sw.WriteLine(strContent);
                    //sw.WriteLine("\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {

            }
        }

        private static string GetRootMapPath(string strPath)
        {
            string rootDir = AppContext.BaseDirectory;

            string dir = string.Empty;
            if (!string.IsNullOrEmpty(strPath))
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart(new char[]
                    {
                    '\\'
                    });
                }
                dir = Path.Combine(rootDir, strPath);
            }
            else
            {
                dir = rootDir;
            }

            return dir;
        }
    }
}
